﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
  public class NguoiDungDAO
    {
      public List<NguoiDungDTO> DanhSachNguoiDung()
      {
          List<NguoiDungDTO> kq = new List<NguoiDungDTO>();
          DataProvider dp = new DataProvider();
          string strsql = "select*from NguoiDung";
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(strsql);
          if (dr.HasRows)
          {
              while (dr.Read())
              {
                  NguoiDungDTO nd = new NguoiDungDTO();
                  nd.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());
                  nd.MatKhau = dr["MatKhau"].ToString();
                  nd.TenDangNhap = dr["TenDangNhap"].ToString();
              }
          }
          return kq;
      }
      public NguoiDungDTO LayNguoiDungByTenDangNhap(string tenDangNhap)
      {
          NguoiDungDTO nd = new NguoiDungDTO();
          string strsql = "select*from NguoiDung where TenDangNhap='" + tenDangNhap + "'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(strsql);
          dr.Read();
          nd.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());
          nd.MatKhau = dr["MatKhau"].ToString();
          nd.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return nd;
      }
      public NguoiDungDTO LayNguoiDungByMaNhanVien(int maNhanVien)
      {
          NguoiDungDTO nd = new NguoiDungDTO();
          string strsql = "select*from NguoiDung where TenDangNhap=" + maNhanVien ;
          DataProvider dp = new DataProvider();
          dp.Connect();
          SqlDataReader dr = dp.ExecuteQuery(strsql);
          dr.Read();
          nd.MaNhanVien = int.Parse(dr["MaNhanVien"].ToString());
          nd.MatKhau = dr["MatKhau"].ToString();
          nd.TenDangNhap = dr["TenDangNhap"].ToString();
          dp.Disconnect();
          return nd;
      }
      public Boolean ThemNguoiDung(NguoiDungDTO nd)
      {
          Boolean kq = false;
          string strsql = "insert into NguoiDung(TenDangNhap,MatKhau,MaNhanVien)";
          strsql += "values('" + nd.TenDangNhap + "','" + nd.MatKhau + "'," + nd.MaNhanVien + ")";

          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;

         
      }
      public Boolean CapNhatNguoiDung(NguoiDungDTO nd)
      {
          Boolean kq = false;
          string strsql = "update NguoiDung set MatKhau='"+nd.MatKhau+"' and MaNhanVien="+nd.MaNhanVien+" where TenDangNhap='"+nd.TenDangNhap+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;
      }
      public Boolean XoaNguoiDung(string tenDangNhap)
      {
          Boolean kq = false;
          string strsql = "delete from NguoiDung where TenDangNhap='"+tenDangNhap+"'";
          DataProvider dp = new DataProvider();
          dp.Connect();
          int i = dp.ExecuteNonQuery(strsql);
          if (i != 0)
              kq = true;
          dp.Disconnect();
          return kq;


      }

    }
}
